package com.exam.dao;

import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class DBUtil {
    private static final String dbUrl;
    private static final String dbUsername;
    private static final String dbPassword;
    private static final String dbDriver;

    static {
        try {
            // 读取配置文件
            Properties properties = new Properties();
            InputStream inputStream = DBUtil.class.getClassLoader().getResourceAsStream("db.properties");
            if (inputStream == null) {
                throw new RuntimeException("db.properties 文件未找到");
            }
            properties.load(inputStream);

            // 读取配置项
            dbUrl = properties.getProperty("db.url");
            dbUsername = properties.getProperty("db.username");
            dbPassword = properties.getProperty("db.password");
            dbDriver = properties.getProperty("db.driver");

            // 加载数据库驱动
            Class.forName(dbDriver);

        } catch (Exception e) {
            throw new RuntimeException("数据库配置加载失败", e);
        }
    }

    // 获取数据库连接
    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(dbUrl, dbUsername, dbPassword);
    }

    // 执行增、删、改操作
    // 手动管理事务的更新方法
    public static int update(Connection connection, String sql, Object... params) throws SQLException {
        try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
            for (int i = 0; i < params.length; i++) {
                preparedStatement.setObject(i + 1, params[i]);
            }
            return preparedStatement.executeUpdate();
        }
    }

    // 查询方法，不关闭 ResultSet
    public static ResultSet query(Connection connection, String sql, Object... params) throws SQLException {
        PreparedStatement stmt = connection.prepareStatement(sql);
        for (int i = 0; i < params.length; i++) {
            stmt.setObject(i + 1, params[i]);
        }
        return stmt.executeQuery();  // 调用者负责关闭 ResultSet 和 PreparedStatement
    }

    // 批量操作方法，支持手动管理事务
    public static void executeBatch(Connection connection, String sql, Object[][] params) throws SQLException {
        try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
            for (Object[] paramSet : params) {
                for (int i = 0; i < paramSet.length; i++) {
                    preparedStatement.setObject(i + 1, paramSet[i]);
                }
                preparedStatement.addBatch();
            }
            preparedStatement.executeBatch();
        }
    }

    // 开启事务
    public static void beginTransaction(Connection connection) throws SQLException {
        if (connection != null) {
            connection.setAutoCommit(false);
        }
    }

    // 提交事务
    public static void commitTransaction(Connection connection) throws SQLException {
        if (connection != null) {
            connection.commit();
        }
    }

    // 回滚事务
    public static void rollbackTransaction(Connection connection) throws SQLException {
        if (connection != null) {
            connection.rollback();
        }
    }
}
